﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using OSC.Base;

namespace OSC.FogBugz
{
    public class FogBugzSocialConnector : ISocialConnector
    {
        public string LogonUrl
        {
            get { return "https://service.orbitone.com"; }
        }

        public string SiteUrl
        {
            get { return "https://service.orbitone.com"; }
        }

        public IPerson Logon(string username, string password, out string connectOut)
        {
            connectOut = "";
            var user = (Environment.UserDomainName + "\\" + Environment.UserName).ToLower();
            using (var context = new FogBugzDataContext())
            {
                return context.Persons.Where(p => p.sLDAPUid.ToLower() == user).Select(ConvertPerson).SingleOrDefault();
            }
        }

        public void Follow(string[] emailAddresses, string displayName)
        {
            throw new NotImplementedException();
        }

        public void Unfollow(string userId)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<IActivity> GetActivities(IPerson person)
        {
            int id = 0;
            if (int.TryParse(person.UserId, out id))
            {
                using (var context = new FogBugzDataContext())
                {
                    return context.BugEvents.Where(ev => ev.ixPerson == id)
                            .Join(context.Bugs,be => be.ixBug, b => b.ixBug,(be, b)=> new {Event = be, Bug = b})
                        .OrderByDescending(ev => ev.Event.dt).Take(30).Select(ev => CreateActivity(person, ev.Event, ev.Bug)).ToList();
                }
            }
            return Enumerable.Empty<IActivity>();
        }

        private IActivity CreateActivity(IPerson person, BugEvent ev, Bug bug)
        {
            var foo = new Activity
                            {
                                ActivityType = ActivityTypes.StatusUpdate,
                                Id = ev.ixBugEvent.ToString(),
                                ApplicationId = 1,
                                Owner = person,
                                PublishDate = ev.dt.HasValue ? ev.dt.Value : DateTime.MinValue
                            };
            foo.AddDetail(
                new Link("Case", "https://service.orbitone.com/?" + ev.ixBug, "CASE ORB-" + ev.ixBug + " : " + ev.sVerb)
                );
            foo.AddDetail(new Text("Content", bug.sTitle));
            return foo;
        }

        public IActivityTemplate GetActivityTemplate()
        {
            return new ActivityTemplate
                       {
                           ApplicationId=1,
                           ActivityType = ActivityTypes.StatusUpdate,
                           Icon = "http://intranet/_layouts/images/ICFOGBUGZ16.gif",
                           Title = "{link:Case} {text:Content}",
                           Data = "{text:Content}"
                       };
        }

        public IEnumerable<IPerson> GetFriends()
        {
            using (var context = new FogBugzDataContext())
            {
                return context.Persons.Select(ConvertPerson).ToList();
            }
        }

        private Expression<Func<Person, Base.IPerson>> ConvertPerson = p
            =>
                   new OSC.Base.Person
                       {
                           UserId = p.ixPerson.ToString(),
                           FullName = p.sFullName,
                           EmailAddresses = new[] { p.sEmail },
                           Company = "Orbit One",
                           LastModificationTime = p.dtLastActivity,
                       };

    }
}
